package com.wty.love;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.wty.love.database.entity.Diary;
import com.wty.love.diary.LookDiaryActivity;

import java.util.List;

public class MainAdapter extends PagerAdapter {
    private Context context;
    private  List<Diary> diaryList;
    private List<View> viewList;
    public MainAdapter(Context context, List<Diary> diaryList, List<View> viewList) {
        this.context = context;
        this.diaryList = diaryList;
        this.viewList = viewList;
    }

    public void setViewList(List<View> viewList){this.viewList = viewList;}
    public void setDiaryList(List<Diary> diaryList){this.diaryList = diaryList;}

    @Override
    public int getCount() {
        return diaryList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = viewList.get(position);

        Diary diary = diaryList.get(position);
        TextView tvPosition = view.findViewById(R.id.position);
        TextView tvNote = view.findViewById(R.id.note);
        TextView tvTitle = view.findViewById(R.id.title);
        TextView tvCreateTime = view.findViewById(R.id.createTime);
        TextView tvWeather = view.findViewById(R.id.weather);
        TextView tvSummary = view.findViewById(R.id.summary);
        FloatingActionButton delete = view.findViewById(R.id.deleteDiary);
        View root = view.findViewById(R.id.root);

        root.setOnClickListener(v->{
            Intent intent = new Intent(context, LookDiaryActivity.class);
            intent.putExtra(LookDiaryActivity.LOOKDIARY,diaryList.get(position));
            intent.putExtra(LookDiaryActivity.NOWPOSITION,position);
            context.startActivity(intent);
        });

        delete.setOnClickListener(v->{
            LiveEventBus.get(MainActivity.DELETEONEDIARY).post(position);
        });

        tvPosition.setText(String.valueOf(position+1));
        tvNote.setText(diary.getNote());
        tvTitle.setText(diary.getTitle());
        tvCreateTime.setText(diary.getCreateTime());
        tvWeather.setText(diary.getWeather());
        String summary = diary.getContent();
        if (summary.length()>20){
            summary = summary.substring(0,20);
        }
        tvSummary.setText( summary );

        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        try {
            container.removeView(viewList.get(position));
        } catch (Exception e) {
        }
    }
}
